package mysql

import (
	"github.com/jmoiron/sqlx"
	"go.uber.org/zap"
	"go_web/logger"
	"go_web/models"
)

func CreateComment(comment *models.Comment) (err error) {
	//1.写出SQL语句
	sqlStr := "insert into comment(comment_id, auth_id, content, post_id, parent_id) value (?, ?, ?, ?, ?) "
	//2.执行SQL语句
	_, err = db.Exec(sqlStr, comment.CommentId, comment.AuthId, comment.Content, comment.PostId, comment.PostId)
	if err != nil {
		logger.Error("insert comment faild", zap.Error(err))
		err = ErrorInsertFailed
		return
	}

	return
}

func GetCommentListByIDs(ids []string) (commentList []*models.Comment, err error) {
	//1.写出SQL语句
	sqlStr := "select comment_id, auth_id, content, post_id, parent_id, create_time from comment where comment_id in (?)"
	//2.执行SQL语句
	query, argu, err := sqlx.In(sqlStr, ids)
	if err != nil {
		return
	}
	query = db.Rebind(query)
	err = db.Select(&commentList, query, argu...)
	return
	//3.返回
}
